﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        public object CreateAB55(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbNABPOS = new DataTable();
            DataTable tbABA = null;
            List<DataRow> list = new List<DataRow>();
            string KRS = "", LFD = "", POS = "", ABPNEUBEN, ABPNEUDAT;
            string VKPKRS = "  ", VKPLFD = "000000", VKPTYE = " ", VKPPOS = "000";
            decimal ABPSOLLMENGE1 = 0, ABPSOLLMENGE2 = 0, ABPSOLLMENGE3 = 0, ABPSOLLMENGE4 = 0, ABPSOLLMENGE5 = 0, ABPSOLLMENGE6 = 0, ABPSOLLMENGE7 = 0,
      ABPSOLLMENGE8 = 0;
            string filter = "";
            DataRow[] rows;
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbABP = db.Tables["ABPOS_EXT"];
            int idx = 0;
            var qABP = from DataRow row in tbABP.AsEnumerable()
                       where row["ABPAUFART"].ToString() == "PR"
                       select row;
            foreach (DataRow dwABP in qABP)
            {
                list = new List<DataRow>();
                MANDANT = dwABP["ABPMANDANT"].ToString();
                ABPNEUBEN = dwABP["ABPNEUBEN"].ToString();
                ABPNEUDAT = dwABP["ABPNEUDAT"].ToString();
                string ABPSTARTDAT = dwABP["ABPSTARTDAT"].ToString();
                string ABPTERMDAT = dwABP["ABPTERMDAT"].ToString();
                KRS = dwABP["ABPAUFKRS"].ToString();
                if (idx == 0)
                {
                    LFD = GetABP(ITXDBVERSION, KRS, ref db, dwABP);
                    list.Add(CreateABK(dwABP, ITXDBVERSION));
                }
                if (LFD == "" || LFD == "000000")
                {
                    throw new Exception("未能成功获取生产单号码:" + KRS);
                }
                idx++;
                POS = Right("000" + dwABP["ABPAUFPOS"].ToString(), 3);
                ABPSOLLMENGE1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString());
                ABPSOLLMENGE2 = Convert.ToDecimal(dwABP["ABPSOLLMENGE2"].ToString());
                ABPSOLLMENGE3 = Convert.ToDecimal(dwABP["ABPSOLLMENGE3"].ToString());
                ABPSOLLMENGE4 = Convert.ToDecimal(dwABP["ABPSOLLMENGE4"].ToString());
                ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString());
                ABPSOLLMENGE6 = Convert.ToDecimal(dwABP["ABPSOLLMENGE6"].ToString());
                ABPSOLLMENGE7 = Convert.ToDecimal(dwABP["ABPSOLLMENGE7"].ToString());
                ABPSOLLMENGE8 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString());
                #region=====ABPOS=========
                DataRow dwBeam = null; ;
                if (tbMU != null)
                {
                    filter = "ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
                        dwBeam = dwReq;
                    }
                }

                EventDispatcher.Instance.RaiseStatus("转换ABPOS" + KRS + LFD + POS, StatusEventType.RunningProgress);
                DataRow dwABPOS = INTEXDBC.Convert2ABPOS(dwABP, ITXDBVERSION);

                dwABPOS["ABPMANDANT"] = MANDANT;
                dwABPOS["ABPAUFKRS"] = KRS;
                dwABPOS["ABPAUFLFD"] = LFD;
                dwABPOS["ABPAUFPOS"] = POS;
                dwABPOS["ABPAUFART"] = dwABP["ABPAUFART"];
                dwABPOS["ABPDIVARTKZ"] = " ";

                dwABPOS["ABPWAKZ"] = dwABP["ABPWAKZ"];
                dwABPOS["ABPART"] = dwABP["ABPART"];
                dwABPOS["ABPVAR"] = dwABP["ABPVAR"];
                dwABPOS["ABPFARBE"] = dwABP["ABPFARBE"];
                dwABPOS["ABPBESCHICH"] = dwABP["ABPBESCHICH"];

                dwABPOS["ABPZLO"] = dwABP["ABPZLO"];
                dwABPOS["ABPZLB"] = dwABP["ABPZLB"];

                dwABPOS["ABPSME"] = dwABP["ABPSME"];
                dwABPOS["ABPSOLLMENGE1"] = dwABP["ABPSOLLMENGE1"];
                dwABPOS["ABPSOLLMENGE2"] = dwABP["ABPSOLLMENGE2"];
                dwABPOS["ABPSOLLMENGE3"] = dwABP["ABPSOLLMENGE3"];
                dwABPOS["ABPSOLLMENGE4"] = dwABP["ABPSOLLMENGE4"];
                dwABPOS["ABPSOLLMENGE5"] = dwABP["ABPSOLLMENGE5"];
                dwABPOS["ABPSOLLMENGE6"] = dwABP["ABPSOLLMENGE6"];
                dwABPOS["ABPSOLLMENGE7"] = dwABP["ABPSOLLMENGE7"];
                dwABPOS["ABPSOLLMENGE8"] = dwABP["ABPSOLLMENGE8"];
                if (dwBeam != null)
                {
                    dwABPOS["ABPRESMGE"] = Convert.ToDouble(dwBeam["ABARESMG"].ToString()) / 10;
                    dwABPOS["ABPRESKG"] = dwBeam["ABARESKG"];
                    dwABPOS["ABPRESSTK"] = dwBeam["ABAREUNIT"];
                }

                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPSTDLAE"] = dwABP["ABPSTDLAE"];
                dwABPOS["ABPMGV"] = dwABP["ABPMGV"];
                dwABPOS["ABPMGB"] = dwABP["ABPMGB"];
                dwABPOS["ABPSZUSATZKZ1"] = "	 ";
                dwABPOS["ABPSZUSATZKZ2"] = "	F";
                dwABPOS["ABPWAART"] = "GEW";
                //有轴生产单决定，在初始化时注意一致
                dwABPOS["ABPRESMGE"] = dwABP["ABPRESMGE"];
                dwABPOS["ABPRESKG"] = dwABP["ABPRESKG"];
                dwABPOS["ABPRESSTK"] = dwABP["ABPRESSTK"];
                dwABPOS["ABPRWKRS"] = dwABP["ABPRWKRS"];
                dwABPOS["ABPRWLFD"] = dwABP["ABPRWLFD"];//获取Cardno
                dwABPOS["ABPANZSTK"] = ABPSOLLMENGE8 / 100;
                dwABPOS["ABPSTKKZ"] = "J";
                dwABPOS["ABPSTKLG"] = "0";
                dwABPOS["ABPANZSTKR"] = "5";
                dwABPOS["ABPFA"] = 0;
                dwABPOS["ABPGA"] = 0;
                dwABPOS["ABPQA"] = 0;

                dwABPOS["ABPZUGETGES"] = ABPSOLLMENGE1;
                dwABPOS["ABPZUTART"] = "	 ";
                dwABPOS["ABPFA"] = "0";
                dwABPOS["ABPKETTAUFT"] = "N";
                dwABPOS["ABPSTKKZ"] = "J";
                dwABPOS["ABPREGEL"] = "	00000000";
                dwABPOS["ABPPOSTYP"] = "   ";
                dwABPOS["ABPWBSTA"] = "60";
                dwABPOS["ABPRWKRS"] = "  ";
                dwABPOS["ABPSTKKZ"] = "J";
                dwABPOS["ABPANZSTKR"] = "0";
                dwABPOS["ABPSZUSATZKZ1"] = " ";
                dwABPOS["ABPSZUSATZKZ2"] = "F";
                dwABPOS["ABPAESTAT"] = "J";
                dwABPOS["ABPRWLFD"] = GetAUFLFD("  ", "WBDAT ");
                dwABPOS["ABPLIEFDAT"] = dwABP["ABPLIEFDAT"];
                dwABPOS["ABPVTDATE"] = dwABP["ABPVTDATE"];
                dwABPOS["ABPSTARTDAT"] = dwABP["ABPSTARTDAT"];
                dwABPOS["ABPENDDAT"] = dwABP["ABPENDDAT"];
                dwABPOS["ABPSTARTDATA"] = dwABP["ABPSTARTDATA"];
                dwABPOS["ABPENDDATA"] = dwABP["ABPENDDATA"];
                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPLIEFDATN"] = dwABP["ABPLIEFDATN"];
                dwABPOS["ABPPOSTYP"] = dwABP["ABPPOSTYP"];
                dwABPOS["ABPTERMDAT"] = dwABP["ABPTERMDAT"];
                dwABPOS["ABPSTARTDATN"] = dwABP["ABPSTARTDATN"];
                dwABPOS["ABPNEUBEN"] = dwABP["ABPNEUBEN"];
                dwABPOS["ABPNEUDAT"] = dwABP["ABPNEUDAT"];

                list.Add(dwABPOS);

                #endregion
                #region=====ABPR=========
                list.Add(CreateABR(dwABPOS, ITXDBVERSION));
                #endregion
                #region====ABAUFM=========
                EventDispatcher.Instance.RaiseStatus("转换ABAUFM" + KRS + LFD, StatusEventType.RunningProgress);
                DataRow dwProd = INTEXDBC.Convert2ABAUFM(null, ITXDBVERSION, false);
                dwProd["ABAMANDANT"] = MANDANT;
                dwProd["ABAAUFTYP"] = "V";
                dwProd["ABAAUFKRS"] = KRS;
                dwProd["ABAAUFLFD"] = LFD;
                dwProd["ABAAUFPOS"] = POS;
                dwProd["ABAAPOS"] = "0001";
                dwProd["ABASGESLG"] = ABPSOLLMENGE1 * 10;
                dwProd["ABASGESGEW"] = ABPSOLLMENGE5 * 10;
                dwProd["ABARSTSLG"] = 0;
                dwProd["ABARSTSGEW"] = 0;
                dwProd["ABALEAPOS"] = "2";
                dwProd["ABAZUPRIO"] = 9;
                dwProd["ABAASSCHNIT"] = "N";
                dwProd["ABAVAKZ"] = "J";
                dwProd["ABALIEFKOMP"] = " ";
                dwProd["ABAWOBST"] = " ";
                dwProd["ABANEUBEN"] = ABPNEUBEN;
                dwProd["ABANEUDAT"] = ABPNEUDAT;
                list.Add(dwProd);
                //Set4ITX.Merge(SResult);
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
                        NewReservaton(dwProd, dwReq, ITXDBVERSION, "AB55MU01", ref list);
                    }
                }
                #endregion
                #region=====ABFOLGE=========
                ConsoldateFlow(dwABPOS, ITXDBVERSION, "AB55", ref list);
                #endregion
                #region=====WDECKZ=========
                EventDispatcher.Instance.RaiseStatus("转换Convert2FDECKZ", StatusEventType.RunningProgress);
                DataRow dwWDECK = INTEXDBC.Convert2WDECK(null, ITXDBVERSION, false);
                dwWDECK["WDMANDANT"] = MANDANT;
                dwWDECK["WDAUFKRS"] = KRS;
                dwWDECK["WDAUFLFD"] = LFD;
                dwWDECK["WDAUFPOS"] = POS;
                dwWDECK["WDTYP"] = "Z";
                dwWDECK["WDWAKZ"] = dwABP["ABPWAKZ"];
                dwWDECK["WDART"] = dwABP["ABPART"];
                dwWDECK["WDFARBE"] = dwABP["ABPFARBE"];
                dwWDECK["WDBESCHICH"] = dwABP["ABPBESCHICH"];
                dwWDECK["WDDAT"] = ABPTERMDAT;
                dwWDECK["WDPRART"] = dwABP["ABPART"];
                dwWDECK["WDPRVAR"] = dwABP["ABPVAR"];
                dwWDECK["WDNEUBEN"] = ABPNEUBEN;
                dwWDECK["WDNEUDAT"] = ABPNEUDAT;
                list.Add(dwWDECK);
                #endregion
                #region=====BAAUF===========
                //BAMANDANT BAAUFTYP BAAUFKRS BAAUFLFD
                filter = " ABAWAKZ='W' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    DataRow dwBAAUF = INTEXDBC.Convert2BAAUF(null, ITXDBVERSION, false);
                    dwBAAUF["BAMANDANT"] = MANDANT;
                    dwBAAUF["BAAUFKRS"] = KRS;
                    dwBAAUF["BAAUFLFD"] = LFD;
                    dwBAAUF["BAAUFPOS"] = POS;
                    dwBAAUF["BANEUBEN"] = row["ABAUSER"]; ;
                    dwBAAUF["BANEUDAT"] = row["ABADATE"]; ;
                    dwBAAUF["BASA"] = row["ABASA"];
                    dwBAAUF["BAPOS"] = "0001";
                    dwBAAUF["BAAFOLGE"] = "010";
                    dwBAAUF["BADATVON"] = dwABP["ABPSTARTDAT"];
                    dwBAAUF["BADATBIS"] = dwABP["ABPSTARTDAT"];
                    dwBAAUF["BAKART"] = row["ABAART"];
                    dwBAAUF["BAKVAR"] = row["ABAVAR"];
                    dwBAAUF["BAKFARBE"] = row["ABAFARBE"];
                    dwBAAUF["BAMTR1"] = Convert.ToDouble(row["ABARESMG"].ToString()) / 10;//轴长度
                    dwBAAUF["BAANZ1"] = row["ABAREUNIT"]; ;
                    dwBAAUF["BAMTR2"] = Convert.ToDouble(row["ABARESMG"].ToString()) / 10;//轴长度
                    dwBAAUF["BAANZ2"] = row["ABAREUNIT"]; ;
                    dwBAAUF["BABAUMART"] = row["ABAUMART"];
                    dwBAAUF["BADISPOKZ"] = row["ABADISPOKZ"];
                    dwBAAUF["BALO"] = row["ABALO"];
                    dwBAAUF["BALB"] = row["ABALB"];
                    dwBAAUF["BALP"] = row["ABALP"];
                    dwBAAUF["BAURKVAR"] = "1 ";
                    dwBAAUF["BADOMKZ"] = "D";
                    dwBAAUF["BABASMG"] = "10000";
                    list.Add(dwBAAUF);
                }
                #endregion
                #region=====GAAUF===========
                filter = " ABAWAKZ='G' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    DataRow dwGAAUF = INTEXDBC.Convert2GAAUF(null, "JLTAB55", false);

                    dwGAAUF["GAMANDANT"] = MANDANT;
                    dwGAAUF["GAAUFKRS"] = KRS;
                    dwGAAUF["GAAUFLFD"] = LFD;
                    dwGAAUF["GAAUFPOS"] = POS;
                    dwGAAUF["GASA"] = row["ABASA"];
                    dwGAAUF["GAPOS"] = row["ABAVKUPOS"];

                    dwGAAUF["GANEUBEN"] = row["ABAUSER"];
                    dwGAAUF["GANEUDAT"] = row["ABADATE"];
                    dwGAAUF["GADATVON"] = dwABP["ABPSTARTDAT"];
                    dwGAAUF["GADATBIS"] = dwABP["ABPSTARTDAT"];
                    dwGAAUF["GAART"] = row["ABAART"];
                    dwGAAUF["GAVAR"] = row["ABAVAR"];
                    dwGAAUF["GAFARBE"] = row["ABAFARBE"];
                    dwGAAUF["GAKG1"] = Convert.ToDouble(row["ABARESMG"].ToString()) / 10;
                    dwGAAUF["GAKG2"] = dwGAAUF["GAKG1"];
                    dwGAAUF["GAAUFTRKZ"] = "A";
                    dwGAAUF["GADISPOKZ"] = row["ABADISPOKZ"];
                    dwGAAUF["GASPUL"] = row["ABAREUNIT"];
                    dwGAAUF["GALAENGE"] = 0;
                    dwGAAUF["GAAFOLGE"] = "010";
                    dwGAAUF["GALO"] = row["ABALO"];
                    dwGAAUF["GALB"] = row["ABALB"];
                    dwGAAUF["GALP"] = row["ABALP"];
                    dwGAAUF["GAAUFBKZ"] = " ";
                    dwGAAUF["GABEM"] = "                ";
                    dwGAAUF["GABASMG2"] = 102000;
                    list.Add(dwGAAUF);
                }
                #endregion
                #region===ICC_ABAX=============
                if (tbMU != null)
                {
                    /*
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
                        foreach (DataRow row in rows)
                        {
                            DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
                            dwABAX["ABAID"] = 0;
                            list.Add(dwABAX);
                        }
                    }
                    */
                }
                #endregion
                SaveAB(ITXDBVERSION, list);
            }
            return list;
        }
    }
}

